package com.aqgj.Organization.service;

import com.aqgj.Organization.bean.Organization;
import com.aqgj.Organization.bean.OrganizationVo;
import com.aqgj.Organization.dao.OrganizationDao;
import com.aqgj.util.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


@Service("Organization")
public class OrganizationManager{
    @Autowired
    OrganizationDao organizationDao;

    public List<Organization> selectTree() {
        List<Organization> organizationList = organizationDao.getAll();
        return organizationList;
    }

    private List<OrganizationVo> getChild(String id, List<OrganizationVo> children){
        // 子菜单
        List<OrganizationVo> childList = new ArrayList<>();
        for (OrganizationVo organizationVo : children) {
            // 遍历所有节点，将父菜单id与传过来的id比较
            if(organizationVo.getPid()!=null && !"".equals(organizationVo.getPid())){
                if (organizationVo.getPid().equals(id)) {
                    childList.add(organizationVo);
                }
            }
        }
        // 把子菜单的子菜单再循环一遍
        for (OrganizationVo organizationVo : childList) {
            // 递归
            organizationVo.setChildren(getChild(organizationVo.getId(), children));
        }
        // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }

    public List<OrganizationVo> selectTreeGrid() {
        List<Organization> organizationList =  organizationDao.getAll();
        List<OrganizationVo> organizationVos = new ArrayList<>(8);
        OrganizationVo organizationVo = null;
        for(Organization organization:organizationList){
            organizationVo = new OrganizationVo();
            organizationVo.setId(organization.getId());
            organizationVo.setPid(organization.getPid());
            organizationVo.setTitle(organization.getName());
            organizationVos.add(organizationVo);
        }
        List<OrganizationVo> organizationOne = new ArrayList<>(8);
        for(OrganizationVo organizationVo1:organizationVos){
            if(organizationVo1.getPid()==null || "".equals(organizationVo1.getPid())){
                organizationOne.add(organizationVo1);

            }
        }
        for(OrganizationVo organizationOne1:organizationOne){
            organizationOne1.setChildren(getChild(organizationOne1.getId(), organizationVos));
        }
        return organizationOne;
    }

    public Boolean insertOrganization(Organization organization) {
        organizationDao.save(organization);
        if(organization.getId()!=null && !"".equals(organization.getId())){
            return true;
        }
        return false;
    }

    public void updateOrganizationById(Organization organization) {
        organizationDao.save(organization);
    }

    public void deleteOrganizationById(String id) {
        organizationDao.removeById(id);
    }

    public Organization getOrganizationById(String id){
        return organizationDao.get(id);
    }
}
